using IdentityServer4;
using IdentityServerHost.Quickstart.UI;
using Microsoft.AspNetCore.HttpOverrides;

using MyIdentity;

using Service.Framework;
using Service.Framework.ConsulExtends;
using Service.Framework.Register;

using System.Security.Cryptography.X509Certificates;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();


var build = builder.Services.AddIdentityServer(options =>
{
    options.Events.RaiseErrorEvents = true;
    options.Events.RaiseInformationEvents = true;
    options.Events.RaiseFailureEvents = true;
    options.Events.RaiseSuccessEvents = true;
    //  options.Discovery.CustomEntries.Add("");

    // see https://identityserver4.readthedocs.io/en/latest/topics/resources.html
    options.EmitStaticAudienceClaim = true;
})
              .AddTestUsers(TestUsers.Users);

// in-memory, code config
build.AddInMemoryIdentityResources(Config.IdentityResources);
build.AddInMemoryApiScopes(Config.ApiScopes);
build.AddInMemoryClients(Config.Clients);
build.AddInMemoryApiResources(Config.ApiResources);

// not recommended for production - you need to store your key material somewhere secure
// builder.AddDeveloperSigningCredential();
// builder.Services.AddScoped<IDiscoveryResponseGenerator,MyDiscoveryResponseGenerator>();

build.AddSigningCredential(new X509Certificate2("/cert/www.liujian520.asia.pfx", "uwjyjc6a"));
builder.Services.AddAuthentication()
   .AddGoogle(options =>
   {
       options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

       // register your IdentityServer with Google at https://console.developers.google.com
       // enable the Google+ API
       // set the redirect URI to https://localhost:5001/signin-google
       options.ClientId = "copy client ID from Google here";
       options.ClientSecret = "copy client secret from Google here";
   });
builder.Services.Configure<ConsulRegisterOptions>(builder.Configuration.GetSection("ConsulSetting"));
builder.Services.AddConsulRegister();

builder.Services.AddSameSiteCookiePolicy();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

await app.Services.GetService<IConsulRegister>()!.ConsulRegistAsync();
app.UseHealthCheckMiddleware();

//���Ҫ�ӣ���Ȼд����cooike1
app.UseCookiePolicy();
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseIdentityServer();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
